<template>
  <div class="main-app">
    <el-form ref="form" :model="form" :rules="rules" label-width="600px" style="margin-top: 50px;">
      <el-form-item label="序号:" prop="code" style="width: 60%;">
        <el-input type="number" v-model="form.code" clearable />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio v-for="(item, index) in options" :key="index" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容:" prop="name" style="width: 60%;">
        <el-input v-model="form.name" type="textarea" :rows="2" clearable style="width: 100%; " />
      </el-form-item>
    </el-form>
    <el-row style="margin-top: 40px; text-align: center">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" @click="save">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import {
  EquipmentStatus
} from "../../../utils/resource";
import {
  save,
  getById
} from "../../../api/system";
export default {
  data() {
    return {
      form: {
        code: "",
        status: "0",
        name: "",
      },
      options: EquipmentStatus,
      rules: {
        code: [{
          required: true,
          message: "请输入序号",
          trigger: "blur"
        }],
        name: [{
          required: true,
          message: "请输入内容",
          trigger: "blur"
        }],
        status: [{
          required: true,
          message: "请选择状态",
          trigger: "change"
        }],
      },
      id: "",
    };
  },
  async created() {
    const { id,type } = this.$route.params;
    await this.getById(id,type);
    this.id = id;

  },
  methods: {
  async getById(id,type) {
    const { data } = await getById({ type: type, code: id });
      this.form.name = data.value;
      this.form.code = data.sort;
      this.form.status = data.status.toString();
    },
    save() {
      this.$refs["form"].validate(async(valid) => {
        if (valid) {
          let data = {
            type: "101",
            sort: parseInt(this.form.code),
            status:this.form.status,
            value: this.form.name,
          };
          let message = "";
          if (!this.id) {
            message = "出入库异常备注新增成功";
          } else {
            data = Object.assign({
              code: this.id
            }, data);
            message = "出入库异常备注修改成功";
          }
          await save(data);
          this.$notify({
            title: "成功",
            message: message,
            type: "success",
            duration: 3000,
          });
          this.$router.push("/system/remark");
        }
      });
    },
    cancel() {
      this.$router.push("/system/remark");
    },
  },
};
</script>
